package com.wei.tang.ui.me;

import com.wei.tang.AppConest;
import com.wei.tang.AppContext;
import com.wei.tang.R;
import com.wei.tang.base.BaseActivity;
import com.wei.tang.bean.UserBean;
import com.wei.tang.db.UserDao;
import com.wei.tang.ui.me.RegistInformationActivity.RegisterTask;
import com.wei.tang.util.Tlog;

import android.content.Intent;
import android.os.AsyncTask;
import android.view.View;
import android.widget.EditText;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 用户找回密码界面
 *
 */
public class RegistForgetActivity extends BaseActivity {

	private RegistForgetActivity mySelf = this;

	@InjectView(R.id.et_username)
	EditText mEtUserName;

	@InjectView(R.id.et_usertag)
	EditText mEtUserTag;

	@InjectView(R.id.et_password)
	EditText mEtPassword;

	private String mUserName = "";
	private String mPassword = "";
	private String mUserTag = "";

	@Override
	public void initView() {
	}

	@Override
	public int getLayoutId() {
		return R.layout.register_forget_activity;
	}

	@Override
	public void initData() {
	}

	@Override
	protected boolean hasBackButton() {
		return true;
	}

	@Override
	protected int getActionBarTitle() {
		return R.string.login_activity;
	}

	@Override
	@OnClick({ R.id.btn_reforget })
	public void onClick(View v) {
		Intent intent = null;
		int id = v.getId();
		switch (id) {
		case R.id.btn_forget:
			handleLogin();
			break;
		}
	}

	private void handleLogin() {

		if (prepareForLogin()) {
			return;
		}

		mUserName = mEtUserName.getText().toString();
		mPassword = mEtPassword.getText().toString();
		mUserTag = mEtUserTag.getText().toString();

		showWaitDialog(R.string.loading);

		RegisterTask registerTask = new RegisterTask();
		registerTask.execute(0);

	}

	class RegisterTask extends AsyncTask<Integer, Integer, Integer> {
		String msg = "";
		int code = AppConest.WS_SUCCESS;

		@Override
		protected Integer doInBackground(Integer... params) {
			UserDao userDao = new UserDao(mySelf);
			code = userDao.changePassword(mUserName, mPassword, mUserTag);
			return code;
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			hideWaitDialog();

			switch (code) {
			case 100:
				msg = "用户不存在，请前往注册!";
				AppContext.showToast(msg);
				break;
			case 200:
				msg = "标示错误重新输入";
				AppContext.showToast(msg);
				break;
			case 300:
				msg = "修改失败 ";
				AppContext.showToast(msg);
				break;
			case 400:
				msg = "修改成功,跳转到登陆";
				AppContext.showToast(msg);
				Intent intent = new Intent(mySelf, RegistLoginActivity.class);
				startActivity(intent);
				break;
			}
		}
	}

	/**
	 * 对输入输入的数据进行检查
	 */
	private boolean prepareForLogin() {
		if (mEtUserName.length() == 0) {
			mEtUserName.setError("请输入手机号码");
			mEtUserName.requestFocus();
			return true;
		}

		if (mEtPassword.length() == 0) {
			mEtPassword.setError("请输入密码");
			mEtPassword.requestFocus();
			return true;
		}

		if (mEtUserTag.length() == 0) {
			mEtUserTag.setError("请输入标示");
			mEtUserTag.requestFocus();
			return true;
		}

		return false;
	}
}
